/**
 * 
 */
package org.tmad.ocms.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author harinath
 * 
 */
@SuppressWarnings("unused")
public class CommonUtils {

	/**
	 * Get the MD5 hash of the given password. This is to make sure no one else
	 * other than the user knows the password, even if the physical system is
	 * compromised.
	 * 
	 * @param password
	 *            the plain text password.
	 * @return the encrypted password
	 * @throws NoSuchAlgorithmException
	 * @throws UnsupportedEncodingException
	 */
	public static String getEncryptedPassword(String password)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		byte[] bytesOfMessage = password.getBytes("UTF-8");

		MessageDigest md = MessageDigest.getInstance("MD5");
		byte[] digest = md.digest(bytesOfMessage);
		BigInteger bigInt = new BigInteger(1, digest);
		String hashtext = bigInt.toString(16);
		return hashtext;
	}

	public static Date getDateFromString(String dateString)
			throws ParseException {

		SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
		return new java.sql.Date(format.parse(dateString).getTime());

	}

	public static boolean validateSession(HttpServletRequest request) {
		if (request.getSession() == null
				|| request.getSession().getAttribute("username") == null) {
			return false;
		}
		return true;

	}
	private void setMenuItems(HttpSession session) {
		Map<String, String> roleBasedMenu = new LinkedHashMap<String,String>();
		roleBasedMenu.put("index.jsp","Home Page");
		roleBasedMenu.put("newCase", "Submit new case");
		roleBasedMenu.put("links.html", "Links");
		
		session.setAttribute("menu", roleBasedMenu);
	}

}
